<template>
	<view class="mainChildAccountAdd">
		<u-navbar title="新增子账号" ></u-navbar>
		<view class="mainChildAccountAdd-content">
			<view class="mainChildAccountAdd-content-li">
				<view class="">
					姓名
				</view>
				<view class="">
					<input type="text" value="" v-model="name"  placeholder="请输入姓名" maxlength="20"/>
				</view>
			</view>
			<view class="mainChildAccountAdd-content-li">
				<view class="">
					手机号
				</view>
				<view class="">
					<input type="number" v-model="iphone" placeholder="请输入手机号" maxlength="11" />
				</view>
			</view>
			<view class="mainChildAccountAdd-content-li">
				<view class="">
					派单金额
				</view>
				<view class=" mainChildAccountAdd-content-li-r" @click="isMoneyshow=true">
					<text>{{IsMoneyinfo.label}}</text>
					<text class="iconfont iconxiangxia"></text>
				</view>
			</view>
			<view class="mainChildAccountAdd-content-li" v-if="IsMoneyinfo.label=='是'">
				<view class="">
					派单总金额
				</view>
				<view class="">
					<input type="number" @input="moneyIpt" @blur="topMoneyChange" value="" v-model="ORMTB417CA07" maxlength="12"/>
				</view>
			</view>
			<view class="mainChildAccountAdd-content-li">
				<view class="">
					派单查看
				</view>
				<view class=" mainChildAccountAdd-content-li-r" @click="sendToViewshow=true">
					<text>{{sendToViewinfo.label}}</text>
					<text class="iconfont iconxiangxia"></text>
				</view>
			</view>
			<view class="mainChildAccountAdd-content-li">
				<view class="">
					派单操作
				</view>
				<view class=" mainChildAccountAdd-content-li-r" @click="sendOperationshow=true" >
					<text>{{sendOperationinfo.label}}</text>
					<text class="iconfont iconxiangxia"></text>
				</view>
			</view>
			<view class="mainChildAccountAdd-content-li">
				<view class="">
					价格查看
				</view>
				<view class=" mainChildAccountAdd-content-li-r" @click="priceToViewshow=true" >
					<text>{{priceToViewinfo.label}}</text>
					<text class="iconfont iconxiangxia"></text>
				</view>
			</view>
			<view class="mainChildAccountAdd-content-li">
				<view class="">
					扫码确认
				</view>
				<view class=" mainChildAccountAdd-content-li-r" @click="priceQRViewshow=true" >
					<text>{{priceQRViewinfo.label}}</text>
					<text class="iconfont iconxiangxia"></text>
				</view>
			</view>
			<view class="mainChildAccountAdd-content-btn">
				<button @click="resultTap">重置</button>
				<button @click="submitTap">保存</button>
			</view>
		</view>
		<u-select v-model="sendToViewshow" :default-value="sendToViewIndex" :list="sendToViewList" @confirm="sendToViewconfirm"></u-select>
		<u-select v-model="sendOperationshow" :default-value="sendOperationIndex" :list="sendOperationList" @confirm="sendOperationconfirm"></u-select>
		<u-select v-model="priceToViewshow" :default-value="priceToViewIndex" :list="priceToViewList" @confirm="priceToViewconfirm"></u-select>
		<u-select v-model="priceQRViewshow" :default-value="priceQRViewIndex" :list="priceQRViewList" @confirm="priceQRViewconfirm"></u-select>
		<u-picker mode="selector" v-model="isMoneyshow" range-key="label" @confirm="moneyfirm" :default-selector="[0]" :range="priceToViewList"></u-picker>
		<u-modal v-model="childShow" :content="childContent" :show-cancel-button="true" @confirm="childConfirm" ></u-modal>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	export default{
		data(){
			return {
				userinfo:JSON.parse(uni.getStorageSync('userinfo')),
				name:'',
				iphone:'',
				ORMTB417CA07:0,
				sendToViewshow:false,
				sendToViewIndex:[0],
				sendToViewList:[
					{
						value: 1,
						label: '全部'
					},
					{
						value: 0,
						label: '自己'
					}
				],
				sendToViewinfo:{
					value: 1,
					label: '全部'
				},
				sendOperationshow:false,
				sendOperationIndex:[0],
				sendOperationList:[
					{
						value: 1,
						label: '是'
					},
					{
						value: 0,
						label: '否'
					}
				],
				sendOperationinfo:{
					value: 1,
					label: '是'
				},
				priceToViewshow:false,
				priceToViewIndex:[0],
				priceToViewList:[
					{
						value: 1,
						label: '是'
					},
					{
						value: 0,
						label: '否'
					}
				],
				priceToViewinfo:{
					value: 1,
					label: '是'
				},
				priceQRViewshow:false,
				priceQRViewIndex:[0],
				priceQRViewList:[
					{
						value: 1,
						label: '是'
					},
					{
						value: 0,
						label: '否'
					}
				],
				priceQRViewinfo:{
					value: 1,
					label: '是'
				},
				isMoneyshow:false,
				IsMoneyinfo:{
					value: 0,
					label: '否'
				},
				childShow:false,
				childContent:'',
				
			}
		},
		methods:{
			resultTap(){
				this.name=''
				this.iphone=''
				this.ORMTB417CA07=0
				this.sendToViewIndex=[0]
				this.sendToViewinfo={
					value: 1,
					label: '全部'
				}
				this.sendOperationIndex=[0]
				this.sendOperationinfo={
					value: 1,
					label: '是'
				}
				this.priceToViewIndex=[0]
				this.priceToViewinfo={
					value: 1,
					label: '是'
				}
				this.priceQRViewIndex=[0]
				this.priceQRViewinfo={
					value: 1,
					label: '是'
				}
				
				
				this.IsMoneyinfo={
					value: 0,
					label: '否'
				}
			},
			sendToViewconfirm(e){
				this.sendToViewIndex = [(this.sendToViewList || []).findIndex((profile) => profile.value === e[0].value)]
				this.sendToViewinfo=e[0]
			},
			sendOperationconfirm(e){
				this.sendOperationIndex = [(this.sendOperationList || []).findIndex((profile) => profile.value === e[0].value)]
				this.sendOperationinfo=e[0]
			},
			priceToViewconfirm(e){
				this.priceToViewIndex = [(this.priceToViewList || []).findIndex((profile) => profile.value === e[0].value)]
				this.priceToViewinfo=e[0]
			},
			priceQRViewconfirm(e){
				this.priceQRViewIndex = [(this.priceQRViewList || []).findIndex((profile) => profile.value === e[0].value)]
				this.priceQRViewinfo=e[0]
			},
			
			
			moneyfirm(e){
				this.IsMoneyinfo=this.priceToViewList[e]
				this.$nextTick(()=>{
					this.ORMTB417CA0=0
				})
			},
			submitTap(){
				var regP = /^1[3-9][0-9]{9}$/;
				if(this.name.trim()==''){
					uni.showToast({
						title: "请输入姓名",
						duration: 5000,
						icon: 'none',
					});
					return 
				} 
				if(!(regP.test(this.iphone))){
					uni.showToast({
						title: "请输入有效手机号",
						duration: 5000,
						icon: 'none',
					});
					return 
				} 
				if(this.ORMTB417CA07===''){
					uni.showToast({
						title: "请输入派单金额",
						duration: 5000,
						icon: 'none',
					});
					return 
				}
				if(this.ORMTB417CA07>=99999999){
					uni.showToast({
						title: "设置金额超过最大限制99999999",
						duration: 5000,
						icon: 'none',
					});
					return 
				}
				this.childShow=true
				this.childContent=`请确认是否绑定该:${this.iphone}为子账号`
			},
			childConfirm(){
				uni.showLoading({
				    title: '加载中...',
					mask:true
				});
				this.childLsit=[]
				uni.request({
					url: this.baseUrl + '/orm/ormtb417/insert', 
					method: 'POST',
					dataType: 'json',
					data: {
						ORMTB417CA00:this.iphone,//子账号手机号
						ORMTB417CA01:this.name,//子账号用户姓名
						ORMTB410ID:this.userinfo.ORMTB410ID,//客户基本信息ID
						ORMTB417CA03:this.sendToViewinfo.value,//派单查看授权0/1(0自己1全部)
						ORMTB417CA05:this.sendOperationinfo.value,//派单操作授权0/1
						ORMTB417CA06:this.priceToViewinfo.value,//价格查看授权0/1
						ORMTB417CA09:this.priceQRViewinfo.value,//扫码确认授权0/1
						ORMTB417CA07:this.ORMTB417CA07,
						ORMTB417CA08:this.IsMoneyinfo.value//派单金额授权0/1
					},
					success: (res) => {
						console.log(res);
						uni.hideLoading();
						if (res.data.code == 200) {
							this.$refs.uToast.show({
								title: '绑定成功',
								type: 'default',
								icon:false
							})
							uni.redirectTo({
								url:'/pages/mainChildAccount/index'
							})
						}else{
							this.$refs.uToast.show({
								title: res.data.message,
								type: 'default',
								icon:false
							})
						}
					},
					fail: (res) => {
						uni.hideLoading();
						this.$refs.uToast.show({
							title: res.errMsg,
							type: 'default',
							icon:false
						})
					}
				});
			},
			moneyIpt(e){
				var num=this.ORMTB417CA07
				num = num.replace(/[^\d.]/g,"");  //清除“数字”和“.”以外的字符
				num = num.replace(/^\./g,""); //验证第一个字符是数字
				num = num.replace(/\.{2,}/g,"."); //只保留第一个. 清除多余的
				num = num.replace(".","$#$").replace(/\./g,"").replace("$#$",".");
				num = num.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3');//只能输入两个小数
				if(num.indexOf(".")< 0 && num !=""){//以上已经过滤，此处控制的是如果没有小数点，首位不能为类似于 01、02的金额
				    num= parseFloat(num);
				}
				this.ORMTB417CA07=''
				this.$nextTick(function(){
					this.ORMTB417CA07=num
				})
			},
			topMoneyChange(){//充值金额失去焦点数据处理
				if(this.ORMTB417CA07==''){
					this.ORMTB417CA07=1
					this.$nextTick(function(){
						this.ORMTB417CA07=0
					})
				}
				var index=String(this.ORMTB417CA07).indexOf('.')
				if(index==-1) return
				var numArr=this.ORMTB417CA07.split('.')
				if(numArr[1].length==0){
					this.ORMTB417CA07=1
					this.$nextTick(function(){
						this.ORMTB417CA07=numArr[0]
					})
				}else{
					this.ORMTB417CA07=1
					this.$nextTick(function(){
						this.ORMTB417CA07=numArr[0]+'.'+numArr[1]
					})
				}
			}
		}
	}
</script>

<style scoped>
	.mainChildAccountAdd{
		width: 100vw;
		min-height: 100vh;
		background-color: #FFF;
	}
	.mainChildAccountAdd-content{
		background-color: #FFF;
		padding: 50rpx 40rpx;
		color: #333;
		font-size: 30rpx;
	}
	.mainChildAccountAdd-content-li{
		display: flex;
		justify-content: space-between;
		line-height: 60rpx;
		border-bottom: 2rpx solid #e3e3e3;
		padding: 30rpx 0 5rpx 0;
	}
	.mainChildAccountAdd-content-li view:first-child{
		flex: .4;
		text-align: right;
	}
	.mainChildAccountAdd-content-li view:last-child{
		flex: 1;
		padding: 0 20rpx 0 40rpx;
	}
	.mainChildAccountAdd-content-li input{
		height: 60rpx;
		line-height: 60rpx;
	}
	.mainChildAccountAdd-content-li-r{
		display: flex;
		justify-content: space-between;
	}
	.mainChildAccountAdd-content-btn{
		display: flex;
		justify-content: space-around;
		margin-top: 100rpx;
	}
	.mainChildAccountAdd-content-btn button{
		width: 150rpx;
		height: 60rpx;
		line-height: 60rpx;
		font-size: 24rpx;
		background-color: #ff976a;
		color: #FFF;
	}
	.mainChildAccountAdd-content-btn button+button{
		background-color: #1989fa;
	}
</style>
